package validator;

import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import command.FilterForm;

@Component
public class FilterFormValidator implements Validator {

	@Override
	public boolean supports(Class<?> clazz) {
		return FilterForm.class.equals(clazz);
	}
	
	@Override
	public void validate(Object target, Errors errors) {
		FilterForm form = (FilterForm) target;
		if(form.getOrderBy() == null ) {
			errors.rejectValue("orderBy", "empty");
		} else if (form.getOrderBy().isEmpty()) {
			form.setOrderBy(null);
		}
	}
}